package practice.tree.four;

/**
 * @Description:
 * @Author Ammar
 * @Create 2023/8/21 19:51
 */
public class TreeNode {
    public Integer val;
    public TreeNode left;
    public TreeNode right;
    public TreeNode parent;

    public TreeNode(Integer val, TreeNode parent) {
        this.val = val;
        this.parent = parent;
    }

    public String toString() {
        String parentStr = "null";
        if (parent != null) {
            parentStr = parent.val.toString();
        }
        return val.toString();
    }

    public boolean isLeftChild() {
        return parent != null && this == parent.left;
    }

    public boolean isRightChild() {
        return parent != null && this == parent.right;
    }

    public boolean hasTwoChildren() {
        return this.left != null && this.right != null;
    }

    public boolean isLeaf() {
        return this.left == null && this.right == null;
    }

    public TreeNode sibling() {
        if (isLeftChild()) return parent.right;
        if (isRightChild()) return parent.left;
        return null;
    }
}
